package com.xunge.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xunge.mapper.ExamMapper;
import com.xunge.pojo.Exam;
import com.xunge.pojo.ExamQueryParam;
import com.xunge.pojo.PageResult;
import com.xunge.service.ExamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;

    /**
     * 分页查询考试信息
     */
    @Override
    public PageResult<Exam> page(ExamQueryParam examQueryParam) {
        // 设置分页参数
        PageHelper.startPage(examQueryParam.getPage(), examQueryParam.getPageSize());
        // 执行查询
        Page<Exam> page = (Page<Exam>) examMapper.list(examQueryParam);
        // 封装分页结果
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 新增考试
     */
    @Override
    public void save(Exam exam) {
        exam.setCreateTime(LocalDateTime.now());
        exam.setUpdateTime(LocalDateTime.now());
        examMapper.insert(exam);
    }

    /**
     * 根据ID查询考试
     */
    @Override
    public Exam getById(Integer id) {
        return examMapper.getById(id);
    }

    /**
     * 根据ID修改考试
     */
    @Override
    public void update(Exam exam) {
        exam.setUpdateTime(LocalDateTime.now());
        examMapper.update(exam);
    }

    /**
     * 根据ID删除考试
     */
    @Override
    public void deleteById(Integer id) {
        examMapper.deleteById(id);
    }

    /**
     * 批量删除考试
     */
    @Override
    public void delete(List<Integer> ids) {
        examMapper.deleteByIds(ids);
    }

    /**
     * 根据课程ID查询考试
     */
    @Override
    public List<Exam> getByCourseId(Integer courseId) {
        return examMapper.getByCourseId(courseId);
    }

    /**
     * 根据班级ID查询考试
     */
    @Override
    public List<Exam> getByClazzId(Integer clazzId) {
        return examMapper.getByClazzId(clazzId);
    }
} 